<template>
  <div>
    <s-table
      ref="table"
      bordered
      size="default"
      row-key="key"
      :columns="columns"
      :data="loadDataCurrent"
      :request="loadDataCurrent"
      :alert="true"
      show-size-changer
      show-quick-jumper
      :show-pagination="false"
    >
      <span
        slot="serial"
        slot-scope="text, record, index"
      >{{ index + 1 }}</span>
    </s-table>
  </div>
</template>
<script>
import { STable } from '@/components'
import { getCurrentProcess } from '@/api/process'
import { getOrgEmployeesListBy } from '@/api/commons'
import JosnBig from 'json-bigint'

const data = []
export default {
    name: 'RebarPlan',
    components: { STable },
    props: {
        state: {
            type: Number
        },
        id: {
            type: String
        }
    },
    data() {
        return {
            columns: [
                {
                    title: '序号',
                    dataIndex: 'serial',
                    width: 80,
                    scopedSlots: { customRender: 'serial' }
                },
                {
                    title: '节点名称',
                    dataIndex: 'taskName'
                },
                {
                    title: '处理人',
                    dataIndex: 'userName'
                },
                {
                    title: '处理结果',
                    dataIndex: 'processStatusName'
                },
                {
                    title: '审批意见',
                    dataIndex: 'opinions',
                    ellipsis: true
                },
                {
                    title: '开始时间',
                    dataIndex: 'createTime'
                },
                {
                    title: '滞留时间',
                    dataIndex: 'durationInMillis'
                },
                {
                    title: '结束时间',
                    dataIndex: 'endTime'
                }
            ],
            loadDataCurrent: () => {
                return getCurrentProcess(this.id).then((res) => {
                    const data = res.data
                    const userIds = []
                    data.forEach((item) => {
                        userIds.push(item.assignee)
                        item.endTime = item.endTime || '进行中'
                        item.durationInMillis = item.durationInMillis || '进行中'
                        if (item.formData) {
                            try {
                                const formDataObj = JosnBig.parse(item.formData)
                                item.opinions = formDataObj.opinions
                                item.auditResult = formDataObj.reject === 'true' ? '审批不通过' : formDataObj.reject === 'false' ? '审批通过' : ''
                            } catch (e) {
                                console.error(e)
                            }
                        } else {
                            item.opinions = ''
                            item.auditResult = ''
                        }
                        item.description = []
                    })
                    return getOrgEmployeesListBy({ userIds }).then((employees) => {
                        this.flowRequesting = false
                        res.data.forEach((r) => {
                            employees.data.some((e) => {
                                if (e.userId === r.assignee) {
                                    r.userName = e.name
                                    return true
                                }
                            })
                        })
                        console.log('数据返回对象：', res)
                        return res
                    })
                })
            }
        }
    }
}
</script>
